<?php
//房态管理
class HousestatsAction extends CommonAction {
		function _filter(&$map) {
				if(!empty($_REQUEST['keywords']) ){
						$map['id'] = array('egt',2);
						$map['account'] = array('like',"%".$_POST['account']."%");
				}
		}

		public function _before_index(){
				//TODO 增加公寓房型
				$this ->assign('hotel',M('Hotel')->where('status = 1')->field('name,id')->findAll());
		}
		public function index(){
				//TODO 优化重复代码及变量
				//$thisMonth = date('m');
				$thisMonth = empty($_POST['month']) ? date('m') : $_POST['month'];
				$year = empty($_POST['month']) ? date('Y') : $_POST['year'];
				//本月
				$firstDay = date("Y-m-d H:i:s",mktime(0, 0 , 0,date("m"),1,date("Y")));
				$lastDay = date("Y-m-d H:i:s",mktime(23,59,59,date("m"),date("t"),date("Y")));
				$monthCount = getdate( strtotime($lastDay) );
				//$monthCount = $monthCount['mday'];
				$monthCount = date(t, strtotime($year.'-'.$thisMonth.'-01'));
				$this->assign('year',$year); 
				$this->assign('month',$thisMonth);
				$this->assign('monthCount',$monthCount);
				$hotel_id = intval($_POST['hotel']);
				$house_id = intval($_POST['house_id']);
				if($_POST['hotel']==null) {//第一次打开默认为圣天地公寓A房型
						$allRooms =  getAvailableRooms(1,'',$year,$thisMonth,1);
						$countRooms = count($allRooms);
						$countFreeRooms = count( getTodayFreeRooms('',1) );
						
						$this->assign("rentSum", sumRentCost('', 1, '','', $year, $thisMonth));
						$this->assign('countRooms', $countRooms );
						$this->assign('rooms', $allRooms );
						$this->assign('freeRooms',$countFreeRooms );
						$this->assign('todayFreeRooms',getTodayFreeRooms('',1) );
				}
				elseif($hotel_id==0) {//查询全部公寓
						$allRooms =  getAvailableRooms('','',$year,$thisMonth,1);
						$countRooms = count($allRooms);
						//统计当天的空房间，去掉月的
						$countFreeRooms = count( getTodayFreeRooms() );

						$this->assign("rentSum", sumRentCost('', '', '', '',$year, $thisMonth));
						$this->assign('countRooms', $countRooms );
						$this->assign('rooms', $allRooms );
						$this->assign('freeRooms',$countFreeRooms );
						$this->assign('todayFreeRooms',getTodayFreeRooms() );
				}
				if($hotel_id>0 && $house_id==0 ) {//查询某个公寓
						$allRooms =  getAvailableRooms('',$hotel_id,$year,$thisMonth,1);
						$countRooms = count($allRooms);
						//统计当天的空房间
						$countFreeRooms = count( getTodayFreeRooms($hotel_id) );
						$this->assign("rentSum", sumRentCost($hotel_id, '', '','', $year, $thisMonth));
						$this->assign('countRooms', $countRooms );
						$this->assign('rooms', $allRooms );
						$this->assign('freeRooms',$countFreeRooms );
						$this->assign('todayFreeRooms',getTodayFreeRooms($hotel_id) );
				}
				if($house_id>0 ) {//查询某个房型
						$allRooms =  getAvailableRooms($house_id, '',$year,$thisMonth,1);
						$countRooms = count($allRooms);
						//统计当天的空房间
						$countFreeRooms = count( getTodayFreeRooms('',$house_id) );
						
						$this->assign("rentSum", sumRentCost('', $house_id, '','', $year, $thisMonth));
						$this->assign('countRooms',$countRooms );
						$this->assign('rooms',$allRooms );
						$this->assign('freeRooms',$countFreeRooms );
						$this->assign('todayFreeRooms',getTodayFreeRooms('',$house_id) );
				}
			 
				//取房子状态的则只从入住单内取
				//预订单3，4状态也要参与，并显示区别

				//统计全店今日空房 = 总数-入住
				$countTodayCheckinRooms = $this->countTodayCheckinRooms();
				$allTodayAvailRooms = count(getAvailableRooms());
				$countTodayFreeRooms = count( getTodayFreeRooms() );
				//统计今天入住房间数
				$this->assign("countTodayCheckinRooms",$countTodayCheckinRooms );
				$this->display();
			 

		}

		//暂时不用，使用getAvailRooms代替
		protected function _countRooms($house_id,$hotel_id){
				$map = array();
				if($house_id!= ''){
						$map['house_id'] = $house_id;
				}
				if($hotel_id!= ''){
						$houseIds= M('House')->where('hotel_id ='.$hotel_id)->field('id')->findAll();
						$house = array();
						 foreach ($houseIds as $k2 => $value) {
								 $house[$k2] = $value['id'];
						 }
						 $house_ids = implode(",", $house);
						 $map['house_id'] = array('in',$house_ids);
						 //$map['status'] = 1;暂时统计全部的房间
						 //TODO 按房间可用时间进行统计
				}
				return M('Room')->where($map)->count('id');
		}
		/**
		 * 统计当日入住的房间数,
		 * 故只需统计状态为“入住中，余款未付”的两个状态即可
		 */
		public function countTodayCheckinRooms($hotel_id='',$house_id=''){
				//当天0点0分0秒
				$curtime = mktime(0, 0, 0, date("n"),date("j"),date("Y") );
				$start_time = 0;
				$end_time = 0;
				$checkin_rooms = 0; //统计当天入住房间数
				//先统计状态不为“入住结束”的入住单
				$map['status'] = array('lt',3);
				if($hotel_id != ''){
						$map['hotel_id'] = $hotel_id;
				}
				 if($house_id != ''){
						$map['house_id'] = $house_id;
				}
				$checkinDetail = D('CheckinView')->where($map)->findAll();
				/*暂去掉时间判断
				foreach ($checkinDetail as $value) {
						$start_time = $value['start_time'];
						if( $value['end_realtime'] != 0 ) { //入住情况，以实际离开时间为准
								$end_time = $value['end_realtime'];
						}else {
								$end_time = $value['end_time'];
						}
						$end_time -= 24*60*60; //减去当天，如入住6到9号，9号上午即要离开，9号可安排人入住
						if( ( ($curtime+24*60*60-1) >= $start_time ) && ($curtime <= $end_time  ) ) {
								//TODO检查入住单中的房间是否在可用范围内
								$checkin_rooms ++;
						}

				}
				*/
				return count($checkinDetail);
		}


}
?>